package com.ohos.carbon.slice.widgets;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.multimodalinput.event.TouchEvent;

import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;
import static ohos.agp.components.InputAttribute.PATTERN_NULL;
import static ohos.agp.components.InputAttribute.PATTERN_PASSWORD;
import static ohos.multimodalinput.event.TouchEvent.PRIMARY_POINT_DOWN;
import static ohos.multimodalinput.event.TouchEvent.PRIMARY_POINT_UP;

public class Text_filedsSlice extends AbilitySlice {

    private Image back_button, textfiled_image_one, textfiled_image_two, textfiled_image_three, textfiled_clear;
    private Text text_title, textfiled_title, text_weight, text_dynamic_one, text_password, text_num_three, textfiled_four_text, emailText;
    private TextField textfiled_one, textfiled_two, textfiled_three, textfiled_four;
    private Component view;
    private int length;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        // getWindow().setInputPanelDisplayType(WindowManager.LayoutConfig.INPUT_ADJUST_RESIZE);
        super.setUIContent(ResourceTable.Layout_ability_text_fileds);
        initView();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Text fields")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Text fields")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Text fields", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Text fields", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Text fields", 1);
            pf.flushSync();
        }
    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        textfiled_two = (TextField) findComponentById(ResourceTable.Id_textfiled_two);
        textfiled_title = (Text) findComponentById(ResourceTable.Id_textfiled_title);
        textfiled_three = (TextField) findComponentById(ResourceTable.Id_textfiled_three);
        textfiled_image_three = (Image) findComponentById(ResourceTable.Id_textfiled_image_three);
        text_password = (Text) findComponentById(ResourceTable.Id_text_password);
        emailText = (Text) findComponentById(ResourceTable.Id_email_text);
        textfiled_image_three.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_visibility));
        textfiled_image_three.setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                switch (touchEvent.getAction()) {
                    case PRIMARY_POINT_DOWN:
                        textfiled_three.setTextInputType(PATTERN_NULL);
                        break;
                    case PRIMARY_POINT_UP:
                        textfiled_three.setTextInputType(PATTERN_PASSWORD);
                        break;
                }
                return true;
            }
        });
        text_num_three = (Text) findComponentById(ResourceTable.Id_text_num_three);
        textfiled_four = (TextField) findComponentById(ResourceTable.Id_textfiled_four);
        textfiled_four_text = (Text) findComponentById(ResourceTable.Id_textfiled_four_text);
        textfiled_four.setText("multi" + "\n" + "line");
        textfiled_clear = (Image) findComponentById(ResourceTable.Id_textfiled_clear);
        textfiled_clear.setClickedListener(component -> textfiled_four.setText(""));
        textfiled_four.setFocusChangedListener((component, b) -> {
            if (b) {
                ShapeElement element = new ShapeElement();
                element.setRgbColor(new RgbColor(23, 153, 139));
                textfiled_four.setBasement(element);
            } else {
                ShapeElement element = new ShapeElement();
                element.setRgbColor(new RgbColor(153, 153, 153));
                textfiled_four.setBasement(element);
            }
        });
        textfiled_clear.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_clear));
        textfiled_three.addTextObserver((s, i, i1, i2) -> {
            length = s.trim().length();
            text_num_three.setText(String.valueOf(length));
        });
        textfiled_three.setFocusChangedListener((component, b) -> {
            if (b) {
                if (length > 0) {
                    AnimatorProperty animatorProperty = text_password.createAnimatorProperty();
                    animatorProperty.stop();
                } else {
                    AnimatorProperty property = text_password.createAnimatorProperty();
                    property.moveFromY(25).moveToY(0).alpha(1).setDuration(200);
                    property.start();
                    text_password.setTextSize(40);
                }
            } else {
                if (length > 0) {
                    AnimatorProperty animatorProperty = text_password.createAnimatorProperty();
                    animatorProperty.stop();
                } else {
                    AnimatorProperty property = text_password.createAnimatorProperty();
                    property.moveFromY(0).moveToY(50).alpha(1).setDuration(200);
                    property.start();
                    text_password.setTextSize(50);
                }

            }
        });
        text_weight = (Text) findComponentById(ResourceTable.Id_text_weight);
        text_dynamic_one = (Text) findComponentById(ResourceTable.Id_text_dynamic_one);
        view = findComponentById(ResourceTable.Id_view);
        textfiled_image_two = (Image) findComponentById(ResourceTable.Id_textfiled_image_two);
        textfiled_image_two.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_clear));
        textfiled_image_two.setClickedListener(component -> textfiled_two.setText(""));
        String mailFormat = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\\\.[a-zA-Z]{2,6}$";
        String regex = "[a-zA-Z0-9_]{1,}[@][a-z0-9]{2,3}[.][a-z]{2,3}";

//        textfiled_two.setTextInputType(1);
        textfiled_two.addTextObserver(new Text.TextObserver() {
            @Override
            public void onTextUpdated(String s, int i, int i1, int i2) {
                text_dynamic_one.setText(String.valueOf(s.trim().length()));

                boolean flag = textfiled_two.getText().matches(regex);
                if (flag) {
                    emailText.setText("correct");
                } else {
                    emailText.setText("it's not an email");
                }
            }
        });

        textfiled_two.setFocusChangedListener((Component component, boolean b) -> {
            if (b) {
                ShapeElement element = new ShapeElement();
                element.setRgbColor(new RgbColor(23, 153, 139));
                textfiled_two.setBasement(element);
//                    textfiled_two.setBackground();
            } else {
                ShapeElement element = new ShapeElement();
                element.setRgbColor(new RgbColor(153, 153, 153));
                textfiled_two.setBasement(element);
            }

        });

        textfiled_image_one = (Image) findComponentById(ResourceTable.Id_textfiled_image_one);
        textfiled_image_one.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_voice));
        textfiled_one = (TextField) findComponentById(ResourceTable.Id_textfiled_one);
        textfiled_one.setFocusChangedListener((component, b) -> {
            if (b) {
                text_weight.setVisibility(Component.VISIBLE);
                ShapeElement element = new ShapeElement();
                element.setRgbColor(new RgbColor(23, 153, 139));
                view.setBackground(element);
            } else {
                text_weight.setVisibility(Component.INVISIBLE);
                ShapeElement element = new ShapeElement();
                element.setRgbColor(new RgbColor(153, 153, 153));
                view.setBackground(element);
            }
        });
        textfiled_title.setTextColor(Color.DKGRAY);
        textfiled_title.setText("$");
        text_title.setVisibility(Component.INVISIBLE);
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        back_button.setClickedListener(component -> onBackPressed());
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
